<?php
/* ******************************
 *	Workers aJax actions
 * ******************************
 */
include('../../includes/classes/core.php');
$action 	= $_REQUEST['act'];
$user_id	= $_SESSION['USERID'];
$error		= '';
$data		= '';

switch ($action) {
	case 'get_add_page':
		$page		= GetPage();
		$data		= array('page'	=> $page);
		
		break;
	case 'get_edit_page':
	    $barcode_id		= $_REQUEST['id'];
		$page		= GetPage(GetBarcode($barcode_id));
        
        $data		= array('page'	=> $page);
        
	    break;
	case 'get_list':
	    $count	= $_REQUEST['count'];
	    $hidden	= $_REQUEST['hidden'];
		$rResult = mysql_query("	SELECT	`districts`.`id`,
											`districts`.`id`,
											`districts`.`name`			
									FROM	`districts`
									WHERE	`districts`.`actived` = 1");
		
		$data = array(
			"aaData"	=> array()
		);
		
		while ( $aRow = mysql_fetch_array( $rResult ) )
		{
			$row = array();
			for ( $i = 0 ; $i < $count ; $i++ )
			{
				/* General output */
				$row[] = $aRow[$i];
				if($i == ($count - 1)){
					$row[] = '<input type="checkbox" name="check_' . $aRow[$hidden] . '" class="check" value="' . $aRow[$hidden] . '" />';
				}
			}
			$row[1] = '№:  '. $row[1];
			$data['aaData'][] = $row;
		}

        break;
    case 'add_district':
		$district_id 	= $_REQUEST['id'];
		$district_name	= $_REQUEST['nm'];		
		$comment	= $_REQUEST['com'];		
		SaveBarcode($district_id, $district_name, $comment);
		
        break;
	case 'get_local_id':
		$data = array('local_id' => GetLocalID() );
        
        break;        
    case 'disable':
		$barcode_id = $_REQUEST['id'];
		Disable($barcode_id);
		
        break;
        
    default:
       $error = 'Action is Null';
}

$data['error'] = $error;

echo json_encode($data);


/* ******************************
 *	Workers Functions
 * ******************************
 */

function SaveBarcode($district_id, $district_name, $comment){
	GLOBAL $error;
	$result = mysql_query("INSERT IGNORE INTO `districts` (`id`) VALUES ($district_id)");
	if(!$result){
		$error = 'Invalid query: ' . mysql_error();
	}else{	
		mysql_query("UPDATE	`districts`
					SET			
							`name`		= '$district_name',
							`comment`	= '$comment'
					WHERE	`id` = '$district_id'");	
	}
}

function Disable($district_id){
    mysql_query("	UPDATE `districts`
					SET    `actived` = 0
					WHERE  `id` = '$district_id'");

	mysql_query("	UPDATE	`postal_codes`
				    SET		`district_id` = ''
				    WHERE	`district_id` = '$district_id'");    

}

function GetLocalID(){
	GLOBAL $db;
	$id = $db->increment('districts');
	return $id;	
}

function GetBarcode($district_id){
    $res = mysql_fetch_assoc(mysql_query("	SELECT	`id`,
													`name`,
    												`comment`			
											FROM	`districts`
											WHERE	`districts`.`id` = '$district_id'"));
	return $res;
}

function GetPage($res = ''){
	$data = '
	<div id="dialog-form">
		    <fieldset style=" width: 432px; ">
		    	<legend>ძირითადი ინფორმაცია</legend>
		    	
		    	<table class="dialog-form-table">
					<tr>
						<td style="width: 70px;"><label for="district_name">უბანი :</label></td>
						<td>
							<input type="text" id="district_name" class="idle seoy-large" onblur="this.className=\'idle seoy-large\'" onfocus="this.className=\'activeField seoy-large\'" value="' . $res['name'] . '" style=" width: 265px !important; "/>
						</td>
					</tr>
					<tr>
						<td style="width: 70px;"><label for="comment">შენიშვნა :</label></td>
						<td valign="top">
							<textarea id="comment" class="idle large" cols="40" rows="3">' . $res['comment'] . '</textarea>
						</td>
					</tr>									
				</table>
	        </fieldset>			
			
			<fieldset style="width: 300px; float: left;">
		    	<legend>საფოსტო ინდექსები</legend>
				
			    <div class="inner-table">
				    <div id="dt_example" class="ex_highlight_row">
				        <div id="container" class="overhead_container">
				        	<div id="button_area" class="inner_buttons">
				        		<button id="add_post_code_button">დამატება</button><button id="delete_post_code_button">წაშლა</button>
				        	</div>
				            <div id="dynamic">
				                <table class="display" id="post_code_list">
				                    <thead>
				                        <tr id="datatable_header">
				                            <th>ID</th>
				                            <th style="width: 100%">ქ. დასახელება</th>									
											<th style="width: 90px">ინდექსი</th>
				                            <th class="check">#</th>
				                        </tr>
				                    </thead>
				                    <thead>
				                        <tr class="search_header">
				                            <th class="colum_hidden">
				                            	<input type="text" name="search_id" value="ფილტრი" class="search_init" />
				                            </th>									
				                            <th>
				                            	<input type="text" name="search_name" value="ფილტრი" class="search_init" />
				                            </th>
				                            <th>
											</th>									
											<th>
												<input type="checkbox" name="check-all-postcode" id="check-all-serv">
											</th>
				                        </tr>
				                    </thead>
				                </table>
				            </div>
				        </div>
				    </div>
				</div>
			<input type="hidden" id="district_id" value="' . $res['id'] . '" />
        </fieldset>
    </div>
    ';
	return $data;
}

?>